From 618137b41904d69cc84569fce264b4a68a2e71ee Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 8 Sep 2014 19:38:32 -0700 Subject: [PATCH] Add proxy configurations --- src/cargo/ops/cargo_upload.rs | 45 ++++++++++++++++++++++++++++++++++- src/cargo/ops/mod.rs | 2 +- src/cargo/sources/registry.rs | 13 +++++++--- src/doc/config.md | 9 +++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_upload.rs b/src/cargo/ops/cargo_upload.rs index 5b30f6759..36f6b6a59 100644 --- a/src/cargo/ops/cargo_upload.rs +++ b/src/cargo/ops/cargo_upload.rs @@ -5,6 +5,7 @@ use std::str; use serialize::json; use curl::http; +use git2; use core::source::Source; use core::{Package, MultiShell, SourceId}; @@ -55,7 +56,7 @@ fn transmit(pkg: &Package, mut tarball: File, let registry_src = SourceId::for_registry(&url); let url = format!("{}/packages/new", host.trim_right_chars('/')); - let mut handle = http::handle(); + let mut handle = try!(http_handle()); let mut req = handle.post(url.as_slice(), &mut tarball) .content_length(stat.size as uint) .content_type("application/x-tar") @@ -129,6 +130,48 @@ pub fn upload_configuration() -> CargoResult { Ok(UploadConfig { host: host, token: token }) } +/// Create a new HTTP handle with appropriate global configuration for cargo. +pub fn http_handle() -> CargoResult { + Ok(match try!(http_proxy()) { + Some(proxy) => http::handle().proxy(proxy), + None => http::handle(), + }) +} + +/// Find a globally configured HTTP proxy if one is available. +/// +/// Favor cargo's `http.proxy`, then git's `http.proxy`, then finally a +/// HTTP_PROXY env var. +pub fn http_proxy() -> CargoResult> { + let configs = try!(config::all_configs(os::getcwd())); + match configs.find_equiv(&"http") { + Some(http) => { + let http = try!(http.table().chain_error(|| { + internal("invalid configuration for the key `http`") + })); + match http.find_equiv(&"proxy") { + Some(proxy) => { + return Ok(Some(try!(proxy.string().chain_error(|| { + internal("invalid configuration for key `http.proxy`") + })).ref0().to_string())) + } + None => {}, + } + } + None => {} + } + match git2::Config::open_default() { + Ok(cfg) => { + match cfg.get_str("http.proxy") { + Ok(s) => return Ok(Some(s.to_string())), + Err(..) => {} + } + } + Err(..) => {} + } + Ok(os::getenv("HTTP_PROXY")) +} + pub fn upload_login(shell: &mut MultiShell, token: String) -> CargoResult<()> { let config = try!(Config::new(shell, None, None)); let UploadConfig { host, token: _ } = try!(upload_configuration()); diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 5f2da921c..ba772460f 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -10,7 +10,7 @@ pub use self::cargo_generate_lockfile::{update_lockfile, load_lockfile}; pub use self::cargo_test::{run_tests, run_benches, TestOptions}; pub use self::cargo_package::package; pub use self::cargo_upload::{upload, upload_configuration, UploadConfig}; -pub use self::cargo_upload::upload_login; +pub use self::cargo_upload::{upload_login, http_proxy, http_handle}; mod cargo_clean; mod cargo_compile; diff --git a/src/cargo/sources/registry.rs b/src/cargo/sources/registry.rs index 9579484ff..f3e34fe1a 100644 --- a/src/cargo/sources/registry.rs +++ b/src/cargo/sources/registry.rs @@ -26,7 +26,7 @@ pub struct RegistrySource<'a, 'b:'a> { cache_path: Path, src_path: Path, config: &'a mut Config<'b>, - handle: http::Handle, + handle: Option, sources: Vec, hashes: HashMap<(String, String), String>, // (name, vers) => cksum } @@ -56,7 +56,7 @@ impl<'a, 'b> RegistrySource<'a, 'b> { src_path: config.registry_source_path().join(part.as_slice()), config: config, source_id: source_id.clone(), - handle: http::Handle::new(), + handle: None, sources: Vec::new(), hashes: HashMap::new(), } @@ -123,8 +123,15 @@ impl<'a, 'b> RegistrySource<'a, 'b> { try!(self.config.shell().status("Downloading", pkg)); try!(fs::mkdir_recursive(&dst.dir_path(), io::UserDir)); + let handle = match self.handle { + Some(ref mut handle) => handle, + None => { + self.handle = Some(try!(ops::http_handle())); + self.handle.as_mut().unwrap() + } + }; // TODO: don't download into memory - let resp = try!(self.handle.get(url.to_string()).exec()); + let resp = try!(handle.get(url.to_string()).exec()); if resp.get_code() != 200 { return Err(internal(format!("Failed to get 200 reponse from {}\n{}", url, resp))) diff --git a/src/doc/config.md b/src/doc/config.md index 721ace2b2..b4c38600e 100644 --- a/src/doc/config.md +++ b/src/doc/config.md @@ -64,4 +64,13 @@ linker = ".." # the `$triple` is being compiled for. ar = ".." linker = ".." + + +# Configuration keys related to the registry +[registry] +host = "..." # URL of the registry (defaults to the central repository) +token = "..." # Access token (found on the central repo's website) + +[http] +proxy = "..." # HTTP proxy to use for HTTP requests (defaults to none) ``` -- 2.30.2